package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.imageview.ShapeableImageView;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.ExchangeMessageBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ActivityHolder> implements View.OnClickListener {

    private Context context;
    private List<ExchangeMessageBean.PageBean.ListBean> mData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener = null;


    public FileAdapter(Context context, List<ExchangeMessageBean.PageBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {
            if (mData.size() > 0) {
//                ExchangeMessageBean.PageBean.ListBean listBean = mData.get(position);
//                String imgUrl = listBean.getContentImg();
//                String title = listBean.getContentTitle();
//                String time = listBean.getCreateTime();
//
//                if (!StringUtils.isEmpty(imgUrl)) {
//                    Glide.with(context)
//                            .load(imgUrl)
//                            .apply(BaseApplication.getRequestOptions())
//                            .into(holder.ivImg);
//                } else {
//                    holder.ivImg.setBackgroundResource(R.mipmap.img_default_news);
//                }
//
//                if (!StringUtils.isEmpty(title)) {
//                    holder.tvTitle.setText(title);
//                } else {
//                    holder.tvTitle.setText("");
//                }
//
//                if (!StringUtils.isEmpty(time)) {
//                    holder.tvTime.setText(time);
//                } else {
//                    holder.tvTitle.setText("");
//                }
            }

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }


    public void upData(List<ExchangeMessageBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ShapeableImageView ivImg;
        @BindView(R.id.tv_file_name)
        TextView tvFileName;
        @BindView(R.id.tv_file_size)
        TextView tvFileSize;
        public ActivityHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
